const daoCommon = require('../util/daoCommon');
const commonUtils = require('../util/commonUtils');
const sqlHelper = require('../util/sqlHelper');

const dao = {
    handleAddService: (data) => {
        let sql;
        const id = commonUtils.genUUID();
        const date = new Date();
        const createTime = date.toLocaleString();
        data.id = id;
        data.createTime = createTime;
        sql = `INSERT INTO service VALUES ($id, $name, $number, $logo,$introduce,$createTime,null,null,null,'0')`;
        return daoCommon.queryCommon(sql, data);
    },
    handleEditService: (data) => {
        let sql;
        const date = new Date();
        const updatedTime = date.toLocaleString();
        data.updatedTime = updatedTime;
        sql = `UPDATE service SET service_name=$name, service_number=$number, service_logo=$logo,service_introduce=$introduce,updated_time=$updatedTime where service_id=$id`;
        return daoCommon.queryCommon(sql, data);
    },
    handleDeleteService: (data) => {
        let is_relate = data.is_relate;
        if (is_relate == 1) {
            let sql = [];
            let sql1 = `UPDATE service SET is_delete='1' where service_id=$id`;
            let sql2 = `UPDATE advertisement SET is_delete='1' where advertisement_target=$id`;
            sql.push(sqlHelper.getNewSql(sql1, data));
            sql.push(sqlHelper.getNewSql(sql2, data));
            return sqlHelper.execTrans(sql);
        } else if (is_relate == 0) {
            let sql = `UPDATE service SET is_delete='1' where service_id=$id`;
            return daoCommon.queryCommon(sql, data);
        }
    },
    handleGetService: () => {
        let sql;
        sql = "select service_id as id,service_name as name,service_number as number,service_logo as logo,service_introduce as introduce from service where is_delete='0' order by service_number";
        return daoCommon.selectCommon(sql);
    },
    handleGetServiceInfo: (data) => {
        let sql;
        sql = "select service_id as id,service_name as name,service_number as number,service_logo as logo,service_introduce as introduce from service where is_delete='0' and service_id=$id";
        return daoCommon.selectCommon(sql, data);
    }
};
module.exports = dao;